package cn.com.nes.site.entity;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageData implements Serializable {
    private static final long serialVersionUID = 1L;
    private int retCode = 9;
    private String retMessage = "操作失败";
    private Integer pagecount;
    private Integer pagesize;
    private Integer pageid;
    private Integer recordcount;
    private UserInfo uinfo;
    private List<Map<String, Object>> page;
    private Map<String, Object> data;
    private Map<String, Object> extraData;
    
    private Page<?> pageData;
    private String key;
    private Object model;
    private String heat_token;
    private Object extData;
    private String firstPageURL;
    private Object datas;

    public static PageData ok() {
        final PageData pageData = new PageData();
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }
    public static PageData ok(String mgs) {
        final PageData pageData = new PageData();
        pageData.setRetCode(0);
        pageData.setRetMessage(mgs);
        return pageData;
    }
    public static PageData ok(List<Map<String, Object>> page) {
        return ok(page, false);
    }

    @SuppressWarnings("unchecked")
    public static PageData ok(List<?> page, boolean convertToMap) {
        final PageData pageData = new PageData();
        if (convertToMap) {
            List<Map<String, Object>> newResults = new ArrayList<>();
            // 将对象转换为map
            if (page != null && page.size() > 0) {
                for (Object obj : page) {
                    Map<String, Object> map = BeanUtil.beanToMap(obj);
                    newResults.add(map);
                }
            }
            pageData.setPage(newResults);
        } else {
            pageData.setPage((List<Map<String, Object>>)page);
        }
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }


    public static PageData ok(Page<Map<String, Object>> page) {
        PageData pageData = new PageData();
        pageData.setPageid(Integer.parseInt(page.getCurrent() + ""));
        pageData.setPagesize(Integer.parseInt(page.getSize() + ""));
        pageData.setPagecount((int) page.getPages());
        pageData.setRecordcount((int) page.getTotal());
        pageData.setPage(page.getRecords());
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }
    public static  PageData okPage(Page<?> page) {
        final  PageData pageData = new PageData();
        pageData.setPageData(page);
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }
    
    
    public static PageData ok(Map<String, Object> data) {
        final PageData pageData = new PageData();
        pageData.setData(data);
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }

    public static PageData ok(String key, Object value) {
        final PageData pageData = new PageData();
        Map<String, Object> data = new HashMap<>();
        data.put(key, value);
        pageData.setData(data);
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }

    @SuppressWarnings("unchecked")
    public static PageData ok(Page<?> page, List<?> results) {
        List<Map<String, Object>> newResults = new ArrayList<>();
        for (Object result : results) {
            if (result instanceof Map) {
                newResults.add((Map<String, Object>) result);
            } else {
                newResults.add(BeanUtil.beanToMap(result));
            }
        }
        PageData pageData = new PageData();
        pageData.setPageid(Integer.parseInt(page.getCurrent() + ""));
        pageData.setPagesize(Integer.parseInt(page.getSize() + ""));
        pageData.setPagecount((int) page.getPages());
        pageData.setRecordcount((int) page.getTotal());
        pageData.setPage(newResults);
        pageData.setRetCode(0);
        pageData.setRetMessage("OK");
        return pageData;
    }

    public static PageData error(String errorMessage) {
        final PageData pageData = new PageData();
        pageData.setRetCode(9);
        pageData.setRetMessage(errorMessage);
        return pageData;
    }

    public String getHeat_token() {
        return heat_token;
    }

    public void setHeat_token(String heat_token) {
        this.heat_token = heat_token;
    }

    public Object getDatas() {
        return datas;
    }

    public void setDatas(Object datas) {
        this.datas = datas;
    }

    public String getFirstPageURL() {
        return firstPageURL;
    }

    public void setFirstPageURL(String firstPageURL) {
        this.firstPageURL = firstPageURL;
    }

    public Object getExtData() {
        return extData;
    }

    public void setExtData(Object extData) {
        this.extData = extData;
    }

    public Object getModel() {
        return model;
    }

    public void setModel(Object model) {
        this.model = model;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getRetCode() {
        return retCode;
    }

    public void setRetCode(int retCode) {
        this.retCode = retCode;
    }

    public String getRetMessage() {
        return retMessage;
    }

    public void setRetMessage(String retMessage) {
        this.retMessage = retMessage;
    }

    public Integer getPagecount() {
        return pagecount;
    }

    public void setPagecount(Integer pagecount) {
        this.pagecount = pagecount;
    }

    public Integer getPagesize() {
        return pagesize;
    }

    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

    public Integer getPageid() {
        return pageid;
    }

    public void setPageid(Integer pageid) {
        this.pageid = pageid;
    }

    public Integer getRecordcount() {
        return recordcount;
    }

    public void setRecordcount(Integer recordcount) {
        this.recordcount = recordcount;
    }

    public UserInfo getUinfo() {
        return uinfo;
    }

    public void setUinfo(UserInfo uinfo) {
        this.uinfo = uinfo;
    }

    public List<Map<String, Object>> getPage() {
        return page;
    }

    public void setPage(List<Map<String, Object>> page) {
        this.page = page;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public Map<String, Object> getExtraData() {
        return extraData;
    }

    public void setExtraData(Map<String, Object> extraData) {
        this.extraData = extraData;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public PageData success() {
        this.setRetCode(0);
        this.setRetMessage("OK");
        return this;
    }

    public PageData error(int code, String errorMessage) {
        this.setRetCode(code);
        this.setRetMessage(errorMessage);
        return this;
    }
    
    public Page<?> getPageData() {
        return pageData;
    }
    
    public void setPageData(Page<?> pageData) {
        this.pageData = pageData;
    }
}